package com.ailk.aee.pcc.utils;

import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author pengzhang
 */
public class DateTimeUtils {

    public static String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_FORMAT = "yyyyMMdd";
    public static String DATE_FORMAT_MM = "yyyyMM";
    public static String DATE_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";


    public static Date addSecondToDateString(String dateString, int value) throws ParseException {
        Date date = stringToDate(dateString, DATE_FORMAT_FULL);
        return DateUtils.addSeconds(date, value);
    }

    public static Date addDay (String dateString,int value, String format) throws ParseException {
        Date date = stringToDate(dateString, format);
        return DateUtils.addDays(date,value);
    }
    public static String addDayForString(String dateString,int value, String format) throws Exception {
        Date date = addDay(dateString, value, format);
        return dateToString(date,format) ;
    }

    public static String dateToString(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static Date stringToDate(String date, String format) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        return simpleDateFormat.parse(date);
    }

    public static boolean isOverSysdate(String dateString) throws ParseException {
        Date date = stringToDate(dateString, DATE_FORMAT_FULL);
        Date sysdate = new Date();
        long l = sysdate.getTime() - date.getTime();
        if (l <= 0) {
            return true;
        }
        return false;
    }

    public static boolean isOverSysdate(String dateString,String format) throws ParseException {
        Date date = stringToDate(dateString, format);
        Date sysdate = new Date();
        long l = sysdate.getTime() - date.getTime();
        if (l <= 0) {
            return true;
        }
        return false;
    }

    public static boolean isSysdateGreaterDate(Date date) {
        Date sysdate = new Date();
        long l = sysdate.getTime() - date.getTime();
        if (l > 0) {
            return true;
        }
        return false;
    }

    public static boolean isSysdateLessDate(Date date) {
        Date sysdate = new Date();
        long l = sysdate.getTime() - date.getTime();
        if (l < 0) {
            return true;
        }
        return false;
    }

    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR);
    }

    public static Date getCurrentTime() {
        Calendar cal = Calendar.getInstance();
        return cal.getTime();
    }
    public static String getCurrentTime (String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }
    public static String getCurrentTimeForString (){
       return dateToString(getCurrentTime(),DATE_FORMAT_FULL)   ;
    }

    public static void main(String[] args) {

        System.out.println(DateTimeUtils.getCurrentTime(DateTimeUtils.DATE_FORMAT));
    }
}
